<template>
    <div class="app-container">
        <el-form ref="queryForm" :model="queryParams" :inline="true" label-width="80px">
            <el-row>
                <el-col :span="6">
                    <el-form-item label="文章名称">
                        <el-input v-model="queryParams.criteria.title" style="width: 250px;"/>
                    </el-form-item>
                </el-col>
                <el-col :span="6">
                    <el-form-item label="作者名称">
                        <el-input v-model="queryParams.criteria.createdName" style="width: 250px;"/>
                    </el-form-item>
                </el-col>
                <el-col :span="6">
                    <el-form-item>
                        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
                        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
                    </el-form-item>
                </el-col>
            </el-row>
        </el-form>

        <el-row :gutter="10" class="mb8">
            <el-col :span="1.5">
                <el-button
                        type="primary"
                        icon="el-icon-plus"
                        size="mini"
                        @click="handleAdd"
                >新增</el-button>
            </el-col>
        </el-row>

        <el-table v-loading="loading" :data="articleList">
            <el-table-column type="selection" width="55" align="center" />
            <el-table-column label="创建日期" align="center" prop="createdDate" width="100">
                <template slot-scope="scope">
                    <span>{{ formatDate(scope.row.createdDate, 'yyyy-MM-dd') }}</span>
                </template>
            </el-table-column>
            <el-table-column label="作者名称" align="center" prop="createdName" width="100"/>
            <el-table-column label="文章名称" align="center" prop="title"/>
            <el-table-column label="标签" align="center" prop="tags"/>
            <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
                <template slot-scope="scope">
                    <el-button
                            size="small"
                            type="primary"
                            icon="el-icon-edit"
                            @click="handleUpdate(scope.row)"
                    >修改</el-button>
                    <el-button
                            size="small"
                            type="success"
                            icon="el-icon-delete"
                            @click="handleDelete(scope.row)"
                    >删除</el-button>
                </template>
            </el-table-column>
        </el-table>

        <pagination
                v-show="total>0"
                :total="total"
                :page.sync="queryParams.pageDto.pageIndex"
                :limit.sync="queryParams.pageDto.pageSize"
                @pagination="getList"
        />
    </div>
</template>

<script>
  import { formatDate} from '@/utils/dateUtil'
  import {getPage ,remove} from '@/api/article'
  export default {
    name: 'index',
    data() {
      return {
        // 遮罩层
        loading: false,
        // 选中数组
        ids: [],
        // 非单个禁用
        single: true,
        // 非多个禁用
        multiple: true,
        // 总条数
        total: 0,
        // 弹出层标题
        title: '',
        // 是否显示弹出层
        open: false,
        isEdit: false,
        // 类型数据字典
        typeOptions: [],

        // 查询参数
        queryParams: {
          pageDto: {
            pageNum: 1,
            pageSize: 20
          },
          criteria: {},
        },
        // 表单参数
        form: {
        },
        // 表单校验
        rules: {},
        articleList:[],
      }


    },
    created() {
      this.getList()

    },
    methods: {
      formatDate,

      /** 查询参数列表 */
      getList(val) {
        this.loading = true
        if(val){
          this.queryParams.pageDto.pageIndex = val.page;
          this.queryParams.pageDto.pageSize = val.limit;
        }
        getPage(this.queryParams).then(response => {
            this.articleList = response.result.list
            this.total = response.result.total
            this.loading = false
          }
        )
      },
      // 取消按钮
      cancel() {
        this.open = false
        this.reset()
      },
      /** 搜索按钮操作 */
      handleQuery() {
        this.queryParams.pageDto.pageIndex = 1
        this.getList()
      },
      /** 重置按钮操作 */
      resetQuery() {
        this.resetForm('queryForm')
        this.handleQuery()
      },
      /** 新增按钮操作 */
      handleAdd() {
        this.$router.push('/article/add');
      },
      /** 删除按钮操作 */
      handleDelete(row) {
        this.$confirm('是否确认删除?', '警告', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(()=> {
          remove(row.id).then(resp=>{
            if(!resp.hasError){
              this.$message.success('操作成功！')
              this.getList();
            }else{
              this.$message.error(resp.message)
            }
          })
        })
      },
      handleUpdate(row){
        sessionStorage.setItem('articleId', row.id);
        this.$router.push('/article/add');
      }
    }
  }
</script>

<style scoped>

</style>
